package com.yjps.system.controller;

import com.alibaba.fastjson.JSON;
import com.yjps.common.message.Message;
import com.yjps.common.page.Pager;
import com.yjps.common.util.ContantsUtil;
import com.yjps.system.model.CodeType;
import com.yjps.system.service.CodeTypeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.naming.ldap.Control;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 码表类控制类
 * Created by jessy on 2015/8/22.
 */
@Controller
@RequestMapping("/system/codetype")
public class CodeTypeController {

    Logger log = LoggerFactory.getLogger(CodeTypeController.class);
    @Autowired
    CodeTypeService codeTypeService;

    /**
     * 分页显示码表类记录数据
     * @param model
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list(Model model, HttpServletRequest request, HttpServletResponse response) throws Exception {
        Pager pager = codeTypeService.getUserByPager(new Pager<CodeType>(request, response));
        List<CodeType> list = pager.getList();
        Map map = (Map) request.getAttribute("map");
        if(map != null)
        for(CodeType c : list){
            if(map.get(c.getCodetypeName()) != null)
            c.setViewName(map.get(c.getCodetypeName()).toString());
        }
        model.addAttribute(pager);
        return "system/basecode/codeTypeList";
    }

    /**
     * 请求添加码表页面
     * @param id
     * @param model
     * @return
     */
    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public String editForm(@RequestParam(value = "id", required = false) String id, Model model) {
        if (!StringUtils.isEmpty(id)) {
            CodeType codetype = codeTypeService.findCodeType(Integer.parseInt(id));
            if (codetype != null) {
                model.addAttribute("codetype", codetype);
            }
        }
        return "system/basecode/edit";
    }

    /**
     * 添加码表类
     * @param codetype
     * @param request
     * @param model
     * @return
     */
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public String edit(CodeType codetype,/* String codetypeNameTemp,String codetypeName,*/HttpServletRequest request, Model model) {
       /* if(StringUtils.isEmpty(codetypeName))
            if(!StringUtils.isEmpty(codetypeNameTemp)){
                CodeType ct = new CodeType();
                ct.setCodetypeName(codetypeNameTemp);
                ct.setIsValid(1);
                ct.setCodetypeState(1);
                ct.setCodetypeDescription("");
                codeTypeService.addCodeType(ct);
            }
            codetype.setCodetypeName(codetypeNameTemp);*/
        codetype.setCodetypeState(1);
        codetype.setIsValid(1);
        codeTypeService.addCodeType(codetype);
        return "redirect:/system/codetype/list";
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(CodeType codetype, Model model) {

        codeTypeService.updateCodeType(codetype);
        return "redirect:/system/codetype/list";
    }

    /**
     * 删除指定id的码表类
     * @param id
     * @param model
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public String delete(@RequestParam(value = "id", required = true) String id, Model model) {
        Message message = null;
        codeTypeService.deleteCodeType(Integer.parseInt(id));
        message = new Message(true, "delete successfully ");
        model.addAttribute("message", message);
        return "redirect:/system/codetype/list";
    }

    /**
     * 获得指定id的码表类
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getCodetype", method = RequestMethod.GET)
    public String getRole(@RequestParam(value = "id", required = true) String id,HttpServletRequest request) {
        CodeType c = codeTypeService.findCodeType(Integer.parseInt(id));
        Map map = (Map) request.getAttribute("map");
        if(map != null)
            if(map.get(c.getCodetypeName()) != null)
                c.setViewName(map.get(c.getCodetypeName()).toString());
        String jsonString = JSON.toJSONString(c);
        return jsonString;
    }

    @ResponseBody
    @RequestMapping(value = "/getCodetypeName",method = RequestMethod.GET)
    public String getCodetypes(HttpServletRequest request){
           List<CodeType> codeTypes = codeTypeService.codetypeList();
        Map map = (Map) request.getAttribute("map");
        if(map != null)
            for(CodeType c : codeTypes){
                if(map.get(c.getCodetypeName()) != null)
                    c.setViewName(map.get(c.getCodetypeName()).toString());
            }
        System.out.println("code Json = " + JSON.toJSONString(codeTypes));
        return JSON.toJSONString(codeTypes);
    }
}
